package com.wanmait.spacesys.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.wanmait.spacesys.dao.MyCollectionDAO;
import com.wanmait.spacesys.dao.UpdateUserinfoCenterDAO;
import com.wanmait.spacesys.vo.Userinfo;

/**
 * 控制层:修改个人信息
 * 
 * @author 28298
 *
 */

@WebServlet("/UpdateUserinfoCenterController")
@MultipartConfig()
public class UpdateUserinfoCenterController extends BaseController {

	// 通过用户id获得用户的信息(登录用户)
	protected void UpdateUserinfoCenterById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
		if (userinfo == null) {
			this.redirect("/web/user/updateinformation.jsp?msg=error", request, response);
		} else {
			UpdateUserinfoCenterDAO.getUpdateUserinfoCenterDAO().userinfoFindById(userinfo.getId());
			int count=MyCollectionDAO.getMyCollectionDAO().getCount(userinfo.getId());
	        request.getSession().setAttribute("count", count);
			this.redirect("/web/user/updateinformation.jsp", request, response);
		}
	}

	
	
	
	// 修改通过用户id获得用户的信息(登录用户)
	protected void getUpdateUserinfoCenter(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得前端的昵称
		String nickname = request.getParameter("nickname");
		// 获得前端的电话号码
		String phone = request.getParameter("phone");
		// 获得前端的头像
		String upload = this.upload(request, response);
		// 获得当前登录用户
		Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
		if (userinfo == null) {
			this.redirect("/web/user/updateinformation.jsp?msg=error", request, response);
		} else {
			userinfo.setNickname(nickname);
			userinfo.setPhone(phone);
			userinfo.setHeaderPic(upload);
			UpdateUserinfoCenterDAO.getUpdateUserinfoCenterDAO().updateUserinfoFindAll(userinfo);

			this.redirect("/UpdateUserinfoCenterController?action=UpdateUserinfoCenterById", request, response);
		}
	}
	
	
	    // 文件上传封装方法
		protected String upload(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			Part part = request.getPart("foodPIC");
		    /* System.out.println(part.toString()); */
			//有上传文件
			if(part.getSize()>0) {
			String names = part.getSubmittedFileName();
			/* System.out.println(names.toString()); */
			String ext = names.substring(names.lastIndexOf('.'));
			String fileName = UUID.randomUUID().toString() + ext;
			InputStream is = part.getInputStream();
			String realPath = this.getServletContext().getRealPath("/images");
			File dir = new File(realPath);
			if (dir.exists()) {
				dir.mkdirs();
			}
			File file = new File(dir, fileName);
			FileOutputStream fos = new FileOutputStream(file);
			byte[] buf = new byte[1024 * 8];
			int len = 0;
			while (true) {
				len = is.read(buf);
				if (len == -1)
					break;
				fos.write(buf, 0, len);
			}
			fos.close();
			is.close();
			/* System.out.println(fileName+"qqq"); */
			return fileName;
			}
		    /* System.out.println("qqq"); */
			return null;
		}



	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.getUpdateUserinfoCenter(request, response);

	}

}
